using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace CollegeShop.Infrastructure.Entities.Repository
{
    [Table("T_Purchasedetail")]
    public class T_Purchasedetail : IEntity<T_Purchasedetail>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }

        public int PurchaseID { get; set; }

        public int GoodsID { get; set; }

        public int PurchaseAmount { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string Unit { get; set; }

        public int PackStandard { get; set; }

        public decimal PurchasePrice { get; set; }

        public decimal PurchaseMoney { get; set; }

        public int PutInAmount { get; set; }

        public override T_Purchasedetail Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.ID == id);
        }

        public override T_Purchasedetail Find(System.Linq.Expressions.Expression<Func<T_Purchasedetail, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Purchasedetail.FirstOrDefault(where);
                if (item != null)
                {
                    this.ID = item.ID;
                    this.PurchaseID = item.PurchaseID;
                    this.GoodsID = item.GoodsID;
                    this.PurchaseAmount = item.PurchaseAmount;
                    this.Unit = item.Unit;
                    this.PackStandard = item.PackStandard;
                    this.PurchasePrice = item.PurchasePrice;
                    this.PurchaseMoney = item.PurchaseMoney;
                    this.PutInAmount = item.PutInAmount;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Purchasedetail, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Purchasedetail.Any(where);
            }
        }
    }
}
